package com.ctp.thread;

import java.util.HashSet;

import org.hraink.futures.ctp.thostftdcuserapistruct.CThostFtdcDepthMarketDataField;
import org.hraink.futures.ctp.thostftdcuserapistruct.CThostFtdcQryExchangeField;
import org.hraink.futures.ctp.thostftdcuserapistruct.CThostFtdcQryInstrumentField;
import org.hraink.futures.jctp.trader.JCTPTraderApi;

import com.ctp.bean.constants.ServerParams;
import com.ctp.display.manager.MarketManager;

public class HeartBeatThread extends Thread implements Runnable {

	private JCTPTraderApi tdApi = null;

	public HeartBeatThread(JCTPTraderApi tdApi) {
		this.tdApi = tdApi;
	}

	public void run() {
		while (true) {
			try {
				requestExchangeStatus();

				CThostFtdcQryInstrumentField aa = new CThostFtdcQryInstrumentField();
				aa.setInstrumentID("au1312");

				tdApi.reqQryInstrument(aa, 0);

				Thread.sleep(1000);
			} catch (Exception e) {

			}
		}
	}

	private void requestExchangeStatus() {
		HashSet<String> exchangeHash = new HashSet<String>();
		for (String instrumentID : ServerParams.INSTRUMENTS) {
			CThostFtdcDepthMarketDataField mdField = MarketManager.instance()
					.getMarketData(instrumentID);
			if (mdField != null) {
				exchangeHash.add(mdField.getExchangeID());
			}
		}

		for (String exchangeID : exchangeHash) {
			CThostFtdcQryExchangeField exField = new CThostFtdcQryExchangeField();
			exField.setExchangeID(exchangeID);

			tdApi.reqQryExchange(exField, 0);
		}
	}

}
